package com.ruifu.proj.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 处理新增和更新的基础数据填充，配合BaseEntity和MyBatisPlusConfig使用
 *
 * @author nyr
 */
@Component
public class MetaHandler implements MetaObjectHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(MetaHandler.class);

    /**
     * 新增数据执行
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        CloudUser cloudUser = SecurityUtils.getUser();
        this.setFieldValByName("creator", cloudUser.getId().longValue(), metaObject);
        this.setFieldValByName("creDate", new Date(), metaObject);
        this.setFieldValByName("lastUpdator", cloudUser.getId().longValue(), metaObject);
        this.setFieldValByName("lastUpdate", new Date(), metaObject);
        this.setFieldValByName("owner", cloudUser.getId().longValue(), metaObject);
        this.setFieldValByName("orgId", cloudUser.getDeptId().longValue(), metaObject);
        this.setFieldValByName("setSid", cloudUser.getTenantId().longValue(), metaObject);
        this.setFieldValByName("delFlag", false, metaObject);
    }

    /**
     * 更新数据执行
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        CloudUser cloudUser = SecurityUtils.getUser();
        this.setFieldValByName("lastUpdator", cloudUser.getId().longValue(), metaObject);
        this.setFieldValByName("lastUpdate", new Date(), metaObject);
    }


}
